Public Class User
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents lblTitle As System.Web.UI.WebControls.Label
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents btnReset As System.Web.UI.WebControls.Button
    Protected WithEvents btnDelete As System.Web.UI.WebControls.Button
    Protected WithEvents hrefParentList As System.Web.UI.WebControls.HyperLink
    Protected WithEvents lblCurrentPage As System.Web.UI.WebControls.Label
    Protected WithEvents hrefUserList As System.Web.UI.WebControls.HyperLink
    Protected WithEvents hrefUserCreateNew As System.Web.UI.WebControls.HyperLink

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Const USER_OBJECT_SESSION_NAME As String = "MIMS_WRS.Web.User.PageBO"

    Protected WithEvents UserControl1 As UserControl
    Protected WithEvents Header1 As Header

    Private CUSTOMER As Byte = 0
    Private INTERNAL As Byte = 1


#Region " Properties"
    Protected Property PageBO() As CUser
        Get
            Return CType(Session(Me.USER_OBJECT_SESSION_NAME), CUser)
        End Get
        Set(ByVal Value As CUser)
            Session(Me.USER_OBJECT_SESSION_NAME) = Value
        End Set
    End Property

    Protected ReadOnly Property userID() As Integer
        Get
            Try
                userID = CInt(Request("id"))
            Catch ex As Exception
                userID = 0
            End Try
        End Get
    End Property

    Private Property usertype() As Byte
        Get
            Return CByte(ViewState("usertype"))
        End Get
        Set(ByVal Value As Byte)
            ViewState("usertype") = Value
        End Set
    End Property

    Private Property Url() As String
        Get
            Return CStr(ViewState("Url"))
        End Get
        Set(ByVal Value As String)
            ViewState("Url") = Value
        End Set
    End Property

    Private Property UrlReferrer() As String
        Get
            Return CStr(ViewState("UrlReferrer"))
        End Get
        Set(ByVal Value As String)
            ViewState("UrlReferrer") = Value
        End Set
    End Property

#End Region


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here        
        If Not Me.IsPostBack Then
            Security.InternalAndAdminOnly()
            Me.Url = Request.Url.PathAndQuery
            Me.UrlReferrer = Request.UrlReferrer.PathAndQuery
            Me.usertype = CByte(Request("usertype")) ' 0:customer; 1:Internal

            'Assign header
            If MIMS_WRS.Web.Security.IsInternal Then
                If MIMS_WRS.Web.Security.IsAdmin Then
                    Header1.NavigationBar.Add("List of Users", AppRoot() + "/Internal/UserList.aspx")
                    Header1.NavigationBar.Add("Create new", AppRoot() + "/Admin/User.aspx?usertype=1")
                    Header1.NavigationBar.Add("List of Customers", AppRoot() + "/Customer/CustomerList.aspx")
                    Header1.NavigationBar.Add("Create new", AppRoot() + "/Admin/User.aspx?usertype=0")
                Else
                    Header1.NavigationBar.Add("List of Customers", AppRoot() + "/Customer/CustomerList.aspx")
                    Header1.NavigationBar.Add("Create new", AppRoot() + "/Admin/User.aspx?usertype=0")
                End If
            End If


            Header1.MenuIndex = EMenuIndex.InternalUserList

            'Load data
            DisplaySubMenu(userID <= 0, Me.usertype)

            Dim obj As New CUser
            If userID > 0 Then
                'When updating ...
                Try
                    obj.Load(userID)
                    If (Me.usertype = 0) Then
                        lblTitle.Text = "UPDATE CUSTOMER"
                    Else
                        lblTitle.Text = "UPDATE USER"
                    End If
                    UserControl1.EnableUserNameControl(False)
                    UserControl1.EnablePasswordControl(False)
                Catch ex As Exception
                    Throw New ApplicationException("Failed to load user", ex)
                End Try
                If obj.UsIdPk <> userID Then
                    Throw New ApplicationException("User invalid. User ID not match")
                End If
            Else
                'When inserting ...
                obj.Init()
                obj.UserType = Me.usertype
                If Me.usertype = 0 Then
                    lblTitle.Text = "CREATE NEW CUSTOMER"
                Else
                    lblTitle.Text = "CREATE NEW USER"
                End If

                btnReset.Visible = False
                btnDelete.Visible = False
            End If

            Me.PageBO = obj
            Me.UserControl1.PageBOSessionName = Me.USER_OBJECT_SESSION_NAME
        End If
    End Sub
    Private Sub DisplaySubMenu(ByVal isNew As Boolean, ByVal userType As Integer)
        DisplayLeftSubMenu(isNew, userType)
        DisplayRightSubMenu(userType)
    End Sub
    Private Sub DisplayLeftSubMenu(ByVal isNew As Boolean, ByVal userType As Integer)
        If userType = 0 Then
            'The User is of Customer Type.
            hrefParentList.Text = "Customer List"
            hrefParentList.NavigateUrl = "../Customer/CustomerList.aspx"
            If isNew Then
                lblCurrentPage.Text = "Create new customer"
            Else
                lblCurrentPage.Text = "Update customer"
            End If
        Else
            'The User is of Internal User Type.
            hrefParentList.Text = "User List"
            hrefParentList.NavigateUrl = "../Internal/UserList.aspx"
            If isNew Then
                lblCurrentPage.Text = "Create new user"
            Else
                lblCurrentPage.Text = "Update user"
            End If
        End If
    End Sub
    Private Sub DisplayRightSubMenu(ByVal userType As Integer)
        If userType = 0 Then
            'The User is of the Customer Type, so we display links to User.
            hrefUserList.Text = "List of Users"
            hrefUserList.NavigateUrl = "../Internal/UserList.aspx"
            hrefUserCreateNew.Text = "Create new"
            hrefUserCreateNew.NavigateUrl = "../Admin/User.aspx?usertype=1"
        Else
            'The User is of the Internal User Type, so we display links to Customer.
            hrefUserList.Text = "List of Customers"
            hrefUserList.NavigateUrl = "../Customer/CustomerList.aspx"
            hrefUserCreateNew.Text = "Create new"
            hrefUserCreateNew.NavigateUrl = "../Admin/User.aspx?usertype=0"
        End If
    End Sub


    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try

            'Dim strCustomer As String = UserControl1.GetCustomerText()
            'Dim strEmail As String = UserControl1.GetEmailText()
            'Dim strLoginName As String = UserControl1.GetLoginNameText()

            'Dim dt As DataTable = CUser.CheckDuplicateData(strCustomer, strEmail, strLoginName)
            'If dt.Rows.Count > 0 Then
            '    Dim row As DataRow
            '    For Each row In dt.Rows
            '        If Not row.Item(0) Is System.DBNull.Value Then
            '            If CStr(row.Item(0)) = PageBO.CustomerCode Then
            '                Me.lblErr.Text = "Code must be unique in the system for customers."
            '                Return
            '            End If
            '        End If
            '        If CStr(row.Item(1)) = PageBO.Email Then
            '            If Me.usertype = Me.CUSTOMER Then
            '                Me.lblErr.Text = "Email must be unique in the system for customers."
            '            Else
            '                Me.lblErr.Text = "Email must be unique in the system for users."
            '            End If
            '            Return
            '        End If
            '        If CStr(row.Item(2)) = PageBO.LoginName Then
            '            If Me.usertype = Me.CUSTOMER Then
            '                Me.lblErr.Text = "Username must be unique in the system for customers."
            '            Else
            '                Me.lblErr.Text = "Username must be unique in the system for users."
            '            End If
            '            Return
            '        End If
            '    Next
            'Else
            UserControl1.UpdateData()
            PageBO.Save()
            If Me.usertype = Me.CUSTOMER Then
                Response.Redirect(AppRoot() + "/Customer/CustomerList.aspx")
            Else
                Response.Redirect(AppRoot() + "/Internal/UserList.aspx")
            End If
            'End If
        Catch ex As Exception
            'Me.lblErr.Text = ex.Message
            UserControl1.SetErrorMessage(ex.Message)
        End Try

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If Me.usertype = Me.CUSTOMER Then
            Response.Redirect(AppRoot() + "/Customer/CustomerList.aspx")
        Else
            Response.Redirect(AppRoot() + "/Internal/UserList.aspx")
        End If
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Response.Redirect(Me.Url)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Me.PageBO.Remove()
        If Me.usertype = Me.CUSTOMER Then
            Response.Redirect(AppRoot() + "/Customer/CustomerList.aspx")
        Else
            Response.Redirect(AppRoot() + "/Internal/UserList.aspx")
        End If
    End Sub
End Class
